<html>
<head>
  <title>../ex5bp.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<a href="../ex5bp.py">download the original source code</a>.
<pre>
<a name="line1">  1</a> <font color="#008000">&quot;&quot;</font><font color="#008000">&quot;Example of using hypre through the Babel-Python interface&quot;</font><font color="#008000">&quot;&quot;</font>
<a name="line2">  2</a> 
<a name="line3">  3</a> <font color="#444444"># To build, do the following</font>
<a name="line4">  4</a> <font color="#444444"># 1. Install Python 2.4 or later.</font>
<a name="line5">  5</a> <font color="#444444"># 2. Install a version of Numeric Python as required by Babel.</font>
<a name="line6">  6</a> <font color="#444444"># 3. Install pyMPI.  Probably this could be made to work with other MPI extensions of Python</font>
<a name="line7">  7</a> <font color="#444444">#    with minor changes in this example; but I haven't tried anything else.</font>
<a name="line8">  8</a> <font color="#444444"># 4. configure --enable-shared --with-babel --enable-python=pyMPI</font>
<a name="line9">  9</a> <font color="#444444">#    If you have trouble with missing stdc++ functions, also use  --without-fei</font>
<a name="line10"> 10</a> <font color="#444444"># 5. make</font>
<a name="line11"> 11</a> <font color="#444444"># 6. Make sure you have the following environment variables:</font>
<a name="line12"> 12</a> <font color="#444444">#    SIDL_DLL_PATH={your top-level hypre directory}/babel/bHYPREClient-P/libbHYPRE.scl</font>
<a name="line13"> 13</a> <font color="#444444">#    LD_LIBRARY_PATH={your top-level hypre directory}/hypre/lib</font>
<a name="line14"> 14</a> 
<a name="line15"> 15</a> <strong>import</strong> <font color="#2040a0">mpi</font>,<font color="#2040a0">sys</font>
<a name="line16"> 16</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>
<a name="line17"> 17</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">MPICommunicator</font>
<a name="line18"> 18</a> <font color="#444444"># It is important to import bHYPRE.MPICommunicator first.  The other classes</font>
<a name="line19"> 19</a> <font color="#444444"># require it, so the build system assumes you will have loaded it first.</font>
<a name="line20"> 20</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IJParCSRMatrix</font>
<a name="line21"> 21</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IJParCSRVector</font>
<a name="line22"> 22</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">BoomerAMG</font>
<a name="line23"> 23</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">PCG</font>
<a name="line24"> 24</a> <strong>import</strong> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IdentitySolver</font>
<a name="line25"> 25</a> <strong>from</strong> <font color="#2040a0">array</font> <strong>import</strong> <font color="4444FF">*</font>
<a name="line26"> 26</a> <strong>from</strong> <font color="#2040a0">struct</font> <strong>import</strong> <font color="4444FF">*</font>
<a name="line27"> 27</a> <strong>from</strong> <font color="#2040a0">Numeric</font> <strong>import</strong> <font color="4444FF">*</font>
<a name="line28"> 28</a> 
<a name="line29"> 29</a> <font color="#2040a0">def</font> <font color="#2040a0">solver</font><font color="4444FF">(</font> <font color="#2040a0">solver_id</font><font color="4444FF">=</font><font color="#FF0000">0</font> <font color="4444FF">)</font><font color="4444FF">:</font>
<a name="line30"> 30</a>     <font color="#444444"># solver_id values : 0(default, AMG), 1 (PCG-AMG), 8 (ParaSails), 50 (PCG)</font>
<a name="line31"> 31</a>     <font color="#444444">#</font>
<a name="line32"> 32</a>     <font color="#2040a0">myid</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">COMM_WORLD</font>.<font color="#2040a0">rank</font>
<a name="line33"> 33</a>     <font color="#2040a0">num_procs</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">COMM_WORLD</font>.<font color="#2040a0">size</font>
<a name="line34"> 34</a>     <font color="#444444">#&gt;&gt;&gt;&gt; doesn't work, but should ...mpi_comm = bHYPRE.MPICommunicator.CreateC( mpi.COMM_WORLD )</font>
<a name="line35"> 35</a>     <font color="#444444">#&gt;&gt;&gt;&gt; temporary substitute&gt;&gt;&gt;</font>
<a name="line36"> 36</a>     <font color="#2040a0">mpi_comm</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">MPICommunicator</font>.<font color="#2040a0">Create_MPICommWorld</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line37"> 37</a>     <font color="#444444">#</font>
<a name="line38"> 38</a>     <font color="#444444">#   for now, don't bother to read any inputs</font>
<a name="line39"> 39</a>     <font color="#444444">#</font>
<a name="line40"> 40</a>     <font color="#2040a0">n</font> <font color="4444FF">=</font> <font color="#FF0000">33</font>
<a name="line41"> 41</a>     <font color="#2040a0">print_solution</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
<a name="line42"> 42</a>     <font color="#444444">#</font>
<a name="line43"> 43</a>     <font color="#444444"># Preliminaries: want at least one processor per row</font>
<a name="line44"> 44</a>     <strong>if</strong> <font color="#2040a0">n</font><font color="4444FF">*</font><font color="#2040a0">n</font> <font color="4444FF">&lt;</font> <font color="#2040a0">num_procs</font> <font color="4444FF">:</font>
<a name="line45"> 45</a>         <font color="#2040a0">n</font> <font color="4444FF">=</font> <font color="#2040a0">int</font><font color="4444FF">(</font> <font color="#2040a0">num_procs</font><font color="4444FF">*</font><font color="4444FF">*</font><font color="#FF0000">0.5</font> <font color="4444FF">)</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line46"> 46</a>     <font color="#2040a0">N</font> <font color="4444FF">=</font> <font color="#2040a0">n</font><font color="4444FF">*</font><font color="#2040a0">n</font>            <font color="#444444"># global number of rows</font>
<a name="line47"> 47</a>     <font color="#2040a0">h</font> <font color="4444FF">=</font> <font color="#FF0000">1.0</font>/<font color="4444FF">(</font><font color="#2040a0">n</font><font color="4444FF">+</font><font color="#FF0000">1</font><font color="4444FF">)</font>      <font color="#444444"># mesh size</font>
<a name="line48"> 48</a>     <font color="#2040a0">h2</font> <font color="4444FF">=</font> <font color="#2040a0">h</font><font color="4444FF">*</font><font color="#2040a0">h</font>
<a name="line49"> 49</a>     <font color="#444444">#</font>
<a name="line50"> 50</a>     <font color="#444444"># Each processor knows only of its own rows - the range is denoted by ilower</font>
<a name="line51"> 51</a>     <font color="#444444"># and upper.  Here we partition the rows. We account for the fact that</font>
<a name="line52"> 52</a>     <font color="#444444"># N may not divide evenly by the number of processors.</font>
<a name="line53"> 53</a>     <font color="#2040a0">local_size</font> <font color="4444FF">=</font> <font color="#2040a0">N</font>/<font color="#2040a0">num_procs</font>
<a name="line54"> 54</a>     <font color="#2040a0">extra</font> <font color="4444FF">=</font> <font color="#2040a0">N</font> <font color="4444FF">-</font> <font color="#2040a0">local_size</font><font color="4444FF">*</font><font color="#2040a0">num_procs</font>
<a name="line55"> 55</a>     <font color="#444444">#</font>
<a name="line56"> 56</a>     <font color="#2040a0">ilower</font> <font color="4444FF">=</font> <font color="#2040a0">local_size</font><font color="4444FF">*</font><font color="#2040a0">myid</font>
<a name="line57"> 57</a>     <font color="#2040a0">ilower</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="#2040a0">min</font><font color="4444FF">(</font><font color="#2040a0">myid</font>, <font color="#2040a0">extra</font><font color="4444FF">)</font>
<a name="line58"> 58</a>     <font color="#444444">#</font>
<a name="line59"> 59</a>     <font color="#2040a0">iupper</font> <font color="4444FF">=</font> <font color="#2040a0">local_size</font><font color="4444FF">*</font><font color="4444FF">(</font><font color="#2040a0">myid</font><font color="4444FF">+</font><font color="#FF0000">1</font><font color="4444FF">)</font>
<a name="line60"> 60</a>     <font color="#2040a0">iupper</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="#2040a0">min</font><font color="4444FF">(</font><font color="#2040a0">myid</font><font color="4444FF">+</font><font color="#FF0000">1</font>, <font color="#2040a0">extra</font><font color="4444FF">)</font>
<a name="line61"> 61</a>     <font color="#2040a0">iupper</font> <font color="4444FF">=</font> <font color="#2040a0">iupper</font> <font color="4444FF">-</font> <font color="#FF0000">1</font>
<a name="line62"> 62</a>     <font color="#444444">#</font>
<a name="line63"> 63</a>     <font color="#444444"># How many rows do I have? </font>
<a name="line64"> 64</a>     <font color="#2040a0">local_size</font> <font color="4444FF">=</font> <font color="#2040a0">iupper</font> <font color="4444FF">-</font> <font color="#2040a0">ilower</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line65"> 65</a>     <font color="#444444">#</font>
<a name="line66"> 66</a>     <font color="#444444"># Create the matrix.</font>
<a name="line67"> 67</a>     <font color="#444444"># Note that this is a square matrix, so we indicate the row partition</font>
<a name="line68"> 68</a>     <font color="#444444"># size twice (since number of rows = number of cols)</font>
<a name="line69"> 69</a>     <font color="#2040a0">parcsr_A</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IJParCSRMatrix</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font>, <font color="#2040a0">ilower</font>, <font color="#2040a0">iupper</font>, <font color="#2040a0">ilower</font>, <font color="#2040a0">iupper</font> <font color="4444FF">)</font>
<a name="line70"> 70</a>     <font color="#444444">#</font>
<a name="line71"> 71</a>     <font color="#444444"># Choose a parallel csr format storage (see the User's Manual)</font>
<a name="line72"> 72</a>     <font color="#444444"># Note: Here the HYPRE interface requires a SetObjectType call.</font>
<a name="line73"> 73</a>     <font color="#444444"># I am using the bHYPRE interface in a way which does not because</font>
<a name="line74"> 74</a>     <font color="#444444"># the object type is already specified through the class name. </font>
<a name="line75"> 75</a>     <font color="#444444">#</font>
<a name="line76"> 76</a>     <font color="#444444"># Initialize before setting coefficients </font>
<a name="line77"> 77</a>     <font color="#2040a0">parcsr_A</font>.<font color="#2040a0">Initialize</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line78"> 78</a>     <font color="#444444">#</font>
<a name="line79"> 79</a>     <font color="#444444"># Now go through my local rows and set the matrix entries.</font>
<a name="line80"> 80</a>     <font color="#444444"># Each row has at most 5 entries. For example, if n=3:</font>
<a name="line81"> 81</a>     <font color="#444444">#</font>
<a name="line82"> 82</a>     <font color="#444444">#   A = [M -I 0; -I M -I; 0 -I M]</font>
<a name="line83"> 83</a>     <font color="#444444">#   M = [4 -1 0; -1 4 -1; 0 -1 4]</font>
<a name="line84"> 84</a>     <font color="#444444">#</font>
<a name="line85"> 85</a>     <font color="#444444"># Note that here we are setting one row at a time, though</font>
<a name="line86"> 86</a>     <font color="#444444">#  one could set all the rows together (see the User's Manual).</font>
<a name="line87"> 87</a>     <font color="#2040a0">values</font> <font color="4444FF">=</font> <font color="#2040a0">zeros</font><font color="4444FF">(</font><font color="#FF0000">5</font><font color="4444FF">)</font>
<a name="line88"> 88</a>     <font color="#2040a0">cols</font> <font color="4444FF">=</font> <font color="#2040a0">zeros</font><font color="4444FF">(</font><font color="#FF0000">5</font><font color="4444FF">)</font>
<a name="line89"> 89</a>     <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#2040a0">ilower</font>
<a name="line90"> 90</a>     <strong>while</strong> <font color="#2040a0">i</font><font color="4444FF">&lt;</font><font color="4444FF">=</font><font color="#2040a0">iupper</font><font color="4444FF">:</font>
<a name="line91"> 91</a>         <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
<a name="line92"> 92</a>         <font color="#444444"># The left identity block: position i-n</font>
<a name="line93"> 93</a>         <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">i</font><font color="4444FF">-</font><font color="#2040a0">n</font><font color="4444FF">)</font> <font color="4444FF">&gt;</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="4444FF">:</font>
<a name="line94"> 94</a>             <font color="#2040a0">cols</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font><font color="4444FF">-</font><font color="#2040a0">n</font>
<a name="line95"> 95</a>             <font color="#2040a0">values</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="4444FF">-</font><font color="#FF0000">1.0</font>
<a name="line96"> 96</a>             <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#2040a0">nnz</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line97"> 97</a>         <font color="#444444"># The left -1: position i-1</font>
<a name="line98"> 98</a>         <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">i</font><font color="4444FF">%</font><font color="#2040a0">n</font><font color="4444FF">)</font><font color="4444FF">:</font>
<a name="line99"> 99</a>             <font color="#2040a0">cols</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font><font color="4444FF">-</font><font color="#FF0000">1</font>
<a name="line100">100</a>             <font color="#2040a0">values</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="4444FF">-</font><font color="#FF0000">1.0</font>
<a name="line101">101</a>             <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#2040a0">nnz</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line102">102</a>         <font color="#444444"># Set the diagonal: position i</font>
<a name="line103">103</a>         <font color="#2040a0">cols</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font>
<a name="line104">104</a>         <font color="#2040a0">values</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#FF0000">4.0</font>
<a name="line105">105</a>         <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#2040a0">nnz</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line106">106</a>         <font color="#444444"># The right -1: position i+1</font>
<a name="line107">107</a>         <strong>if</strong> <font color="4444FF">(</font><font color="4444FF">(</font><font color="#2040a0">i</font><font color="4444FF">+</font><font color="#FF0000">1</font><font color="4444FF">)</font><font color="4444FF">%</font><font color="#2040a0">n</font><font color="4444FF">)</font><font color="4444FF">:</font>
<a name="line108">108</a>             <font color="#2040a0">cols</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font><font color="4444FF">+</font><font color="#FF0000">1</font>
<a name="line109">109</a>             <font color="#2040a0">values</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="4444FF">-</font><font color="#FF0000">1.0</font>
<a name="line110">110</a>             <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#2040a0">nnz</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line111">111</a>         <font color="#444444"># The right identity block:position i+n</font>
<a name="line112">112</a>         <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">i</font><font color="4444FF">+</font><font color="#2040a0">n</font><font color="4444FF">)</font> <font color="4444FF">&lt;</font> <font color="#2040a0">N</font><font color="4444FF">:</font>
<a name="line113">113</a>             <font color="#2040a0">cols</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">i</font><font color="4444FF">+</font><font color="#2040a0">n</font>
<a name="line114">114</a>             <font color="#2040a0">values</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="4444FF">-</font><font color="#FF0000">1.0</font>
<a name="line115">115</a>             <font color="#2040a0">nnz</font> <font color="4444FF">=</font> <font color="#2040a0">nnz</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line116">116</a>         <font color="#444444"># Set the values for row i</font>
<a name="line117">117</a>         <font color="#2040a0">parcsr_A</font>.<font color="#2040a0">SetValues</font><font color="4444FF">(</font> <font color="#2040a0">array</font><font color="4444FF">(</font><font color="4444FF">[</font><font color="#2040a0">nnz</font><font color="4444FF">]</font><font color="4444FF">)</font>, <font color="#2040a0">array</font><font color="4444FF">(</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font><font color="4444FF">)</font>, <font color="#2040a0">cols</font>, <font color="#2040a0">values</font> <font color="4444FF">)</font>
<a name="line118">118</a>         <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#2040a0">i</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line119">119</a>     <font color="#444444">#</font>
<a name="line120">120</a>     <font color="#444444"># Assemble after setting the coefficients</font>
<a name="line121">121</a>     <font color="#2040a0">parcsr_A</font>.<font color="#2040a0">Assemble</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line122">122</a>     <font color="#444444">#</font>
<a name="line123">123</a>     <font color="#444444"># Create the rhs and solution</font>
<a name="line124">124</a>     <font color="#2040a0">par_b</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IJParCSRVector</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font>, <font color="#2040a0">ilower</font>, <font color="#2040a0">iupper</font> <font color="4444FF">)</font>
<a name="line125">125</a>     <font color="#2040a0">par_b</font>.<font color="#2040a0">Initialize</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line126">126</a>     <font color="#2040a0">par_x</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IJParCSRVector</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font>, <font color="#2040a0">ilower</font>, <font color="#2040a0">iupper</font> <font color="4444FF">)</font>
<a name="line127">127</a>     <font color="#2040a0">par_x</font>.<font color="#2040a0">Initialize</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line128">128</a>     <font color="#444444">#</font>
<a name="line129">129</a>     <font color="#444444"># Set the rhs values to h^2 and the solution to zero</font>
<a name="line130">130</a>     <font color="#2040a0">rhs_values</font> <font color="4444FF">=</font> <font color="#2040a0">zeros</font><font color="4444FF">(</font><font color="#2040a0">local_size</font><font color="4444FF">)</font><font color="4444FF">*</font><font color="#FF0000">1.1</font>
<a name="line131">131</a>     <font color="#2040a0">x_values</font> <font color="4444FF">=</font> <font color="#2040a0">zeros</font><font color="4444FF">(</font><font color="#2040a0">local_size</font><font color="4444FF">)</font><font color="4444FF">*</font><font color="#FF0000">1.1</font>
<a name="line132">132</a>     <font color="#2040a0">rows</font> <font color="4444FF">=</font> <font color="#2040a0">zeros</font><font color="4444FF">(</font><font color="#2040a0">local_size</font><font color="4444FF">)</font>
<a name="line133">133</a>     <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
<a name="line134">134</a>     <strong>while</strong> <font color="#2040a0">i</font><font color="4444FF">&lt;</font><font color="#2040a0">local_size</font><font color="4444FF">:</font>
<a name="line135">135</a>         <font color="#2040a0">rhs_values</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">h2</font>
<a name="line136">136</a>         <font color="#2040a0">rows</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">ilower</font> <font color="4444FF">+</font> <font color="#2040a0">i</font>
<a name="line137">137</a>         <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#2040a0">i</font> <font color="4444FF">+</font> <font color="#FF0000">1</font>
<a name="line138">138</a>     <font color="#2040a0">par_b</font>.<font color="#2040a0">SetValues</font><font color="4444FF">(</font> <font color="#2040a0">rows</font>, <font color="#2040a0">rhs_values</font> <font color="4444FF">)</font>
<a name="line139">139</a>     <font color="#2040a0">par_x</font>.<font color="#2040a0">SetValues</font><font color="4444FF">(</font> <font color="#2040a0">rows</font>, <font color="#2040a0">x_values</font> <font color="4444FF">)</font>
<a name="line140">140</a>     <font color="#444444">#</font>
<a name="line141">141</a>     <font color="#2040a0">par_b</font>.<font color="#2040a0">Assemble</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line142">142</a>     <font color="#2040a0">par_x</font>.<font color="#2040a0">Assemble</font><font color="4444FF">(</font><font color="4444FF">)</font>
<a name="line143">143</a>     <font color="#444444">#</font>
<a name="line144">144</a>     <font color="#444444"># Choose a solver and solve the system</font>
<a name="line145">145</a>     <font color="#444444">#</font>
<a name="line146">146</a>     <font color="#444444"># AMG</font>
<a name="line147">147</a>     <strong>if</strong> <font color="#2040a0">solver_id</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
<a name="line148">148</a>         <font color="#444444"># Create solver</font>
<a name="line149">149</a>         <font color="#2040a0">solver</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">BoomerAMG</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font>, <font color="#2040a0">parcsr_A</font> <font color="4444FF">)</font>
<a name="line150">150</a>         <font color="#444444"># Set some parameters (See Reference Manual for more parameters)</font>
<a name="line151">151</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;PrintLevel&quot;</font>, <font color="#FF0000">3</font> <font color="4444FF">)</font>  <font color="#444444"># print solve info + parameters</font>
<a name="line152">152</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;CoarsenType&quot;</font>, <font color="#FF0000">6</font> <font color="4444FF">)</font> <font color="#444444"># Falgout coarsening</font>
<a name="line153">153</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;RelaxType&quot;</font>, <font color="#FF0000">3</font> <font color="4444FF">)</font>   <font color="#444444"># G-S/Jacobi hybrid relaxation</font>
<a name="line154">154</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;NumSweeps&quot;</font>, <font color="#FF0000">1</font> <font color="4444FF">)</font>   <font color="#444444"># Sweeeps on each level</font>
<a name="line155">155</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;MaxLevels&quot;</font>, <font color="#FF0000">20</font> <font color="4444FF">)</font>  <font color="#444444"># maximum number of levels</font>
<a name="line156">156</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetDoubleParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;Tolerance&quot;</font>, <font color="#FF0000">1e-7</font> <font color="4444FF">)</font>      <font color="#444444"># conv. tolerance</font>
<a name="line157">157</a>         <font color="#444444">#</font>
<a name="line158">158</a>         <font color="#444444"># Now setup and solve!</font>
<a name="line159">159</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">Setup</font><font color="4444FF">(</font> <font color="#2040a0">par_b</font>, <font color="#2040a0">par_x</font> <font color="4444FF">)</font>
<a name="line160">160</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">Apply</font><font color="4444FF">(</font> <font color="#2040a0">par_b</font>, <font color="#2040a0">par_x</font> <font color="4444FF">)</font>
<a name="line161">161</a>         <font color="#444444">#</font>
<a name="line162">162</a>         <font color="#444444"># Run info - needed logging turned on</font>
<a name="line163">163</a>         <font color="#444444"># The 0-th return value of Get*Value is the error flag.</font>
<a name="line164">164</a>         <font color="#2040a0">num_iterations</font> <font color="4444FF">=</font> <font color="#2040a0">solver</font>.<font color="#2040a0">GetIntValue</font><font color="4444FF">(</font> <font color="#008000">&quot;NumIterations&quot;</font> <font color="4444FF">)</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>
<a name="line165">165</a>         <font color="#2040a0">final_res_norm</font> <font color="4444FF">=</font> <font color="#2040a0">solver</font>.<font color="#2040a0">GetDoubleValue</font><font color="4444FF">(</font> <font color="#008000">&quot;RelResidualNorm&quot;</font> <font color="4444FF">)</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>
<a name="line166">166</a>         <font color="#444444">#</font>
<a name="line167">167</a>         <strong>if</strong> <font color="#2040a0">myid</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
<a name="line168">168</a>             <strong>print</strong> <font color="#008000">&quot;Iterations = &quot;</font>, <font color="#2040a0">num_iterations</font>
<a name="line169">169</a>             <strong>print</strong> <font color="#008000">&quot;Final Relative Residual Norm = &quot;</font>, <font color="#2040a0">final_res_norm</font>
<a name="line170">170</a>     <strong>elif</strong> <font color="#2040a0">solver_id</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">50</font><font color="4444FF">:</font>
<a name="line171">171</a>         <font color="#444444"># Create solver</font>
<a name="line172">172</a>         <font color="#2040a0">solver</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">PCG</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font>, <font color="#2040a0">parcsr_A</font> <font color="4444FF">)</font>
<a name="line173">173</a>         <font color="#444444"># Set some parameters (See Reference Manual for more parameters)</font>
<a name="line174">174</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;MaxIter&quot;</font>, <font color="#FF0000">1000</font> <font color="4444FF">)</font> <font color="#444444"># max iterations</font>
<a name="line175">175</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetDoubleParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;Tolerance&quot;</font>, <font color="#FF0000">1e-7</font> <font color="4444FF">)</font> <font color="#444444"># conv. tolerance</font>
<a name="line176">176</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;TwoNorm&quot;</font>, <font color="#FF0000">1</font> <font color="4444FF">)</font> <font color="#444444"># use the two norm as the stopping criteria</font>
<a name="line177">177</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;PrintLevel&quot;</font>, <font color="#FF0000">2</font> <font color="4444FF">)</font> <font color="#444444"># prints out the iteration info</font>
<a name="line178">178</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetIntParameter</font><font color="4444FF">(</font> <font color="#008000">&quot;Logging&quot;</font>, <font color="#FF0000">1</font> <font color="4444FF">)</font> <font color="#444444"># needed to get run info later</font>
<a name="line179">179</a>         <font color="#444444">#</font>
<a name="line180">180</a>         <font color="#2040a0">precond</font> <font color="4444FF">=</font> <font color="#2040a0">bHYPRE</font>.<font color="#2040a0">IdentitySolver</font>.<font color="#2040a0">Create</font><font color="4444FF">(</font> <font color="#2040a0">mpi_comm</font> <font color="4444FF">)</font>
<a name="line181">181</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">SetPreconditioner</font><font color="4444FF">(</font> <font color="#2040a0">precond</font> <font color="4444FF">)</font>
<a name="line182">182</a>         <font color="#444444">#</font>
<a name="line183">183</a>         <font color="#444444"># Now setup and solve!</font>
<a name="line184">184</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">Setup</font><font color="4444FF">(</font> <font color="#2040a0">par_b</font>, <font color="#2040a0">par_x</font> <font color="4444FF">)</font>
<a name="line185">185</a>         <font color="#2040a0">solver</font>.<font color="#2040a0">Apply</font><font color="4444FF">(</font> <font color="#2040a0">par_b</font>, <font color="#2040a0">par_x</font> <font color="4444FF">)</font>
<a name="line186">186</a>         <font color="#444444">#</font>
<a name="line187">187</a>         <font color="#444444"># Run info - needed logging turned on</font>
<a name="line188">188</a>         <font color="#444444"># The 0-th return value of Get*Value is the error flag.</font>
<a name="line189">189</a>         <font color="#2040a0">num_iterations</font> <font color="4444FF">=</font> <font color="#2040a0">solver</font>.<font color="#2040a0">GetIntValue</font><font color="4444FF">(</font> <font color="#008000">&quot;NumIterations&quot;</font> <font color="4444FF">)</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>
<a name="line190">190</a>         <font color="#2040a0">final_res_norm</font> <font color="4444FF">=</font> <font color="#2040a0">solver</font>.<font color="#2040a0">GetDoubleValue</font><font color="4444FF">(</font> <font color="#008000">&quot;RelResidualNorm&quot;</font> <font color="4444FF">)</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>
<a name="line191">191</a>         <font color="#444444">#</font>
<a name="line192">192</a>         <strong>if</strong> <font color="#2040a0">myid</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#FF0000">0</font><font color="4444FF">:</font>
<a name="line193">193</a>             <strong>print</strong> <font color="#008000">&quot;Iterations = &quot;</font>, <font color="#2040a0">num_iterations</font>
<a name="line194">194</a>             <strong>print</strong> <font color="#008000">&quot;Final Relative Residual Norm =&quot;</font>, <font color="#2040a0">final_res_norm</font>
<a name="line195">195</a>     <strong>else</strong><font color="4444FF">:</font>
<a name="line196">196</a>         <strong>print</strong> <font color="#008000">&quot;Solver &quot;</font>, <font color="#2040a0">solver_id</font>, <font color="#008000">&quot; is not supported.&quot;</font>
<a name="line197">197</a> <font color="#444444">#</font>
<a name="line198">198</a> <strong>if</strong> <font color="#2040a0">__name__</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
<a name="line199">199</a>     <font color="#2040a0">solver</font><font color="4444FF">(</font><font color="#FF0000">0</font><font color="4444FF">)</font>

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
